package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotWeatherDictService;
import com.lechi.web.system.domain.IotWeatherDeviceBo;
import com.lechi.web.system.domain.IotWeatherDict;
import com.lechi.web.system.domain.IotWeatherSensorParseBo;
import com.lechi.web.system.mapper.iot.IotWeatherDictMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 气象观测业务分类及编码Service业务层处理
 *
 * @author yangguang
 */
@Service
@RequiredArgsConstructor
public class IotWeatherDictServiceImpl implements IIotWeatherDictService {

    private final IotWeatherDictMapper iotWeatherDictMapper;

    /**
     * 查询气象观测业务分类及编码
     *
     * @param id 气象观测业务分类及编码主键
     * @return 气象观测业务分类及编码
     */
    @Override
    public IotWeatherDict selectIotWeatherDictById(Long id) {
        try {
            return iotWeatherDictMapper.selectIotWeatherDictById(id);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询气象观测配置", method, id));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 查询气象观测业务分类及编码列表
     *
     * @param deviceId 气象观测业务分类及编码主键
     * @return 气象观测业务分类及编码
     */
    @Override
    public List<IotWeatherDeviceBo> selectIotWeatherDeviceList(Long deviceId) {
        try {
            return iotWeatherDictMapper.selectIotWeatherDeviceList(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量查询气象观测设备配置", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 查询气象观测业务分类及编码列表
     *
     * @param deviceId 气象观测业务分类及编码主键
     * @return 气象观测业务分类及编码
     */
    @Override
    public List<IotWeatherSensorParseBo> selectIotWeatherSensorList(Long deviceId) {
        try {
            return iotWeatherDictMapper.selectIotWeatherSensorList(deviceId);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量查询气象观测要求配置", method, deviceId));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 新增气象观测业务分类及编码
     *
     * @param iotWeatherDict 气象观测业务分类及编码
     */
    @Override
    public void insertIotWeatherDict(IotWeatherDict iotWeatherDict) {
        try {
            iotWeatherDictMapper.insertIotWeatherDict(iotWeatherDict);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增气象观测配置", method, iotWeatherDict));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改气象观测业务分类及编码
     *
     * @param iotWeatherDict 气象观测业务分类及编码
     * @return 结果
     */
    @Override
    public int updateIotWeatherDict(IotWeatherDict iotWeatherDict) {
        try {
            return iotWeatherDictMapper.updateIotWeatherDict(iotWeatherDict);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改气象观测配置", method, iotWeatherDict));
            throw new ServiceException(ErrorCodeEnum.UPDATE_ERROR.getValue(), ErrorCodeEnum.UPDATE_ERROR.getKey());
        }
    }

    /**
     * 批量删除气象观测业务分类及编码
     *
     * @param ids 需要删除的气象观测业务分类及编码主键
     */
    @Override
    public void deleteIotWeatherDictByIds(Long[] ids) {
        try {
            iotWeatherDictMapper.deleteIotWeatherDictByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除气象观测配置", method, ids));
            throw new ServiceException(ErrorCodeEnum.DELETE_ERROR.getValue(), ErrorCodeEnum.DELETE_ERROR.getKey());
        }
    }
}
